package com.ruoyi.wechat.controller;

import com.ruoyi.wechat.common.Constants;
import com.ruoyi.wechat.common.ServiceException;
import com.ruoyi.wechat.domain.Order;
import com.ruoyi.wechat.domain.dto.OrderDTO;
import com.ruoyi.wechat.domain.form.PlaceForm;
import com.ruoyi.wechat.domain.vo.AjaxResult;
import com.ruoyi.wechat.service.IOrderService;
import com.ruoyi.wechat.service.impl.WxPayServiceImpl;
import com.ruoyi.wechat.utils.LoginHelper;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 订单  参考文档地址：  https://blog.csdn.net/weixin_61428498/article/details/134728436
 * 小程序端拉取支付文档地址： https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_4.shtml
 * </p>
 *
 * @author YinHeng
 * @since 2024/2/6
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private IOrderService orderService;

    @Autowired
    private WxPayServiceImpl wxPayService;

    /**
     * 取件下单
     *
     * @return
     */
    @PostMapping("/pick/place")
    public AjaxResult pickPlace(@RequestBody @Validated(PlaceForm.PickPlace.class) PlaceForm req) {
        LoginHelper.checkAuth();
        //校验取件信息
        if (ObjectUtils.isEmpty(req.getAttachText()) && ObjectUtils.isEmpty(req.getAttachFileList())) {
            throw new ServiceException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "请文字描述取件信息或上传取件信息截图");
        }
        //校验配送方式
        if (Constants.Order.DELV_MODE_3.getValue().equals(req.getDelvMode()) && ObjectUtils.isEmpty(req.getIntoPwd())) {
            throw new ServiceException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "请填写临时入户密码");
        }
        //预约时间
        if (Constants.Order.DELV_MODE_4.getValue().equals(req.getDelvMode()) && ObjectUtils.isEmpty(req.getAptTime())) {
            throw new ServiceException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "请选择预约时间");
        }

        Order order = orderService.place(req);
        return AjaxResult.success(order);
    }

    /**
     * 服务下单
     *
     * @return
     */
    @PostMapping("/serv/place")
    public AjaxResult servPlace(@RequestBody @Validated(PlaceForm.ServPlace.class) PlaceForm req) {
        LoginHelper.checkAuth();
        //设置为小程序支付
        Order order = orderService.place(req);
        return AjaxResult.success(order);
    }


    /**
     * 下单
     *
     * @return
     */
    @PostMapping("/item/place")
    public AjaxResult itemPlace(@RequestBody @Validated(PlaceForm.ItemPlace.class) PlaceForm req) {
        LoginHelper.checkAuth();
        Order order = orderService.place(req);
        return AjaxResult.success(order);
    }

    /**
     * 查询订单
     *
     * @param form
     * @return
     */
    @GetMapping("/list")
    public AjaxResult searchList(Order form) {
        List<OrderDTO> orderList = orderService.searchList(form);
        return AjaxResult.success(orderList, "ok");
    }


    /**
     * 查询订单
     *
     * @param form
     * @return
     */
    @GetMapping("/detail")
    public AjaxResult detail(Order form) {
        OrderDTO dto = orderService.detail(form);
        return AjaxResult.success(dto, "ok");
    }

}
